package com.xutong.myfunction.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.xutong.myfunction.R;
import com.xutong.myfunction.Utils.HttpUtils;
import com.xutong.myfunction.Utils.ImageDownloadUtils;
import com.xutong.myfunction.entity.Data;
import com.xutong.myfunction.entity.ResultData;

import java.util.ArrayList;
import java.util.HashMap;

public class HttpTestActivity extends AppCompatActivity {

    private HashMap<String,String> hashMap;
    private ImageView ivShow;
    private ImageView ivShow2;
    private ImageView ivShow3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http_test);

        ImageDownloadUtils.init(this);

        ivShow = (ImageView) findViewById(R.id.ivShow);
        ivShow2 = (ImageView) findViewById(R.id.ivShow2);
        ivShow3 = (ImageView) findViewById(R.id.ivShow3);
        hashMap = new HashMap<>();

    }

    public void IntentOnClick(View v){

        switch (v.getId()){
            case R.id.btnHttpGet:
                MyTaskHttpGet myTaskHttpGet = new MyTaskHttpGet();
                myTaskHttpGet.execute();
                break;
            case R.id.btnHttpPost:
                hashMap.put("account","13727010001");
                hashMap.put("password","123456");
                MyTaskHttpPost myTaskHttpPost = new MyTaskHttpPost();
                myTaskHttpPost.execute();
                break;
            case R.id.btnHttpGetDownload:

                ImageDownloadUtils.disPlayImage("http://pic1.win4000.com/wallpaper/d/56678bcbac9fb.jpg",ivShow2,R.drawable.loading,R.drawable.err);

                ImageDownloadUtils.disPlayImage("http://p4.so.qhmsg.com/t01edffb3b1f55488.jpg",ivShow,R.drawable.loading,R.drawable.err);

                ImageDownloadUtils.disPlayImage("http://pic1.win4000.com/wallpaper/f/56da433ce5458.jpg",ivShow3,R.drawable.loading,R.drawable.err);

                break;
        }

    }

    class MyTaskHttpGet extends AsyncTask<String,Void,Boolean>{

        ResultData<ArrayList<Data>> resultData = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Boolean doInBackground(String... params) {

            resultData = HttpUtils.get(hashMap);
            if (resultData!=null){
                return resultData.getState();
            }
            return false;
        }

        @Override
        protected void onPostExecute(Boolean b) {
            super.onPostExecute(b);
            if (b){
//                Log.d("resultData",resultData.getData().get(0).toString());
                Data data = resultData.getData().get(0);
                Log.d("Data",data.toString());
            }
        }
    }


    class MyTaskHttpPost extends AsyncTask<String,Void,Integer>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected Integer doInBackground(String... params) {

            HttpUtils.post(hashMap);

            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);

        }
    }


}
